<template>
  <div class="consultation">
    <h2>审核管理</h2>
    <div class="consultation-block">
      <div class="table" v-loading="tableLoading">
        <el-table size="mini" :data="tableDataFilter" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="jobName" label="职位名称"> </el-table-column>
          <el-table-column prop="salary" label="薪资"> </el-table-column>
          <el-table-column prop="jobLocation" label="职位地点"> </el-table-column>
          <el-table-column prop="tags" label="标签" :formatter="formatTags"> </el-table-column>
          <el-table-column prop="experience" label="经验"> </el-table-column>
          <el-table-column prop="jobDesc" label="岗位描述"> </el-table-column>
          <el-table-column prop="other" label="其他信息"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="warning"
                size="mini"
                @click="updateData(scope.row)"
                >审核</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="pageNum"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="审核"
      :visible.sync="isVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        status-icon
        ref="form"
        label-width="120px"
        class="demo-form"
      >
        <el-form-item label="职位名称" prop="jobName">
          <el-input disabled type="text" v-model="form.jobName"></el-input>
        </el-form-item>
        <el-form-item label="岗位描述" prop="jobDesc">
          <el-input disabled type="textarea" :rows="4" v-model="form.jobDesc"></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-checkbox-group disabled v-model="form.tags">
            <el-checkbox :key="index" v-for="(tag, index) in jobTags" :label="tag"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="薪资" prop="salary">
          <el-select disabled v-model="form.salary" placeholder="请选择">
            <el-option
              v-for="item in jobSalarys"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经验" prop="experience">
          <el-select disabled v-model="form.experience" placeholder="请选择">
            <el-option
              v-for="item in jobExperiences"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位地点" prop="jobLocation">
          <el-select disabled v-model="form.jobLocation" placeholder="请选择">
            <el-option
              v-for="item in jobLocations"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">通过</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addJob, getJob, deleteJob, updateJob,getJobSalary,getJobTags,getJobExperience,getJobLocation } from "../service.js";
export default {
  name: "home",
  beforeCreate() {
    getJobSalary().then(res => {
      this.jobSalarys = res.code === 200 ? res.data : []
    })
    getJobTags().then(res => {
      this.jobTags = res.code === 200 ? res.data : []
    })
    getJobExperience().then(res => {
      this.jobExperiences = res.code === 200 ? res.data : []
    })
    getJobLocation().then(res => {
      this.jobLocations = res.code === 200 ? res.data : []
    })
  },
  mounted() {
    this.refreshTable();
  },
  data() {
    return {
      tableData: [],
      tableDataFilter: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      isVisible: false,
      dialogTitle: "",
      form: {
        jobName: "",
        jobDesc: "",
        tags: [],
        salary: '',
        experience: '',
        jobLocation: '',
      },
      inputVisible: false,
      columnModel: [],
      funList: [],
      jobSalarys: [],
      jobTags: [],
      jobExperiences: [],
      jobLocations: []
    };
  },
  methods: {
    naviData() {
      this.tableDataFilter = this.tableData.slice(
        (this.pageNum - 1) * this.pageSize,
        this.pageSize * this.pageNum
      );
    },
    handleAvatarSuccess(info) {
      this.form.pics.push(info.url);
    },
    addUser() {
      this.isVisible = true;
      this.form = {
        username: "",
        pass: "",
        checkPass: "",
        title: "",
        column: "",
      };
      this.dialogTitle = "新建用户";
    },
    handleClose() {
      this.isVisible = false;
    },
    handleCurrentChange(index) {
      this.pageNum = index
      this.naviData()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateJob(this.form._id, {
            examine: 'true'
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success("审核成功");
              this.refreshTable();
              this.isVisible = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    addData() {
      this.dialogTitle = "新增";
      this.isVisible = true;
    },
    refreshTable() {
      getJob().then((res) => {
        this.tableData = res.code === 200 ? res.data.filter(v => !v.examine) : [];
        for (let item of this.tableData) {
          item.tags = JSON.parse(item.tags)
        }
        this.total = this.tableData.length;
        this.naviData()
      });
    },
    updateData(row) {
      this.form = {
        ...row
      };
      this.dialogTitle = "修改";
      this.isVisible = true;
    },
    deleteData(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteJob(row._id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              if (this.tableDataFilter.length == 1) {
                this.pageNum--
              }
              this.refreshTable();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteImg(index) {
      this.form.pics.splice(index, 1);
    },
    formatTags(row) {
      if (row.tags) {
        return row.tags.join(',')
      }
    }
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 60px;
  height: 60px;
  line-height: 60px;
  border: 1px dashed gray;
  text-align: center;
}
.avatar {
  width: 60px;
  height: 60px;
  display: block;
}
.img-blocks {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.el-icon-circle-close {
  position: absolute;
  top: -5px;
  right: -5px;
  cursor: pointer;
  color: red;
}
</style>
